package com.lemon.smartsafety.ui.activity.safe.data;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.event.datacoll.BuildListItemEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafetynew.ui.activity.base.BaseSearchFragmentActivity;
import com.lemon.smartsafety.ui.fragment.safe.data.BuildingListFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by Cooliean on 16/1/24.
 * 数据采集员 《检查项目管理》【建筑列表】
 */
public class BuildingCheckItemBuildingListActivity extends BaseSearchFragmentActivity {
    private LoggerHelper loggerHelper = new LoggerHelper(BuildingCheckItemBuildingListActivity.class);

    private BuildingListFragment mFragment;
    private CompanyInfo          mCompanyInfo;


    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            mCompanyInfo = (CompanyInfo) getIntent().getSerializableExtra(BundleKey.COMPANY_INFO);
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
    }

    @Override
    public Fragment getFragment() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
        mFragment = BuildingListFragment.newInstance();
        mFragment.setArguments(bundle);
        return mFragment;
    }


    public Fragment getFragment(String text) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.KEY_WORD, text);
        bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
        mFragment = BuildingListFragment.newInstance();
        mFragment.setArguments(bundle);
        return mFragment;
    }

    @Override
    public int getSearchHintRes() {
        return R.string.search_common_hit;
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.safe_data_building_list);
    }

    @Override
    public boolean onQueryTextSubmit(String queryText) {
        queryText = StringUtils.nullValue(queryText);
        getSupportFragmentManager().beginTransaction().replace(R.id.main_content, getFragment(queryText), getFragment(queryText).getClass().getName()).commit();
        return super.onQueryTextSubmit(queryText);
    }

    @Subscribe
    public void onEventMainThread(BuildListItemEvent event) {
        if (BuildingListFragment.class.getName().equals(event.getTag())) {
            if (mCompanyInfo == null) {
                mHUDUtil.showErrorTip(getString(R.string.params_error));
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COMPANY_BUILD_INFO, event.getData());
            bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
            UIHelper.showActivity(BuildingCheckItemBuildingListActivity.this, bundle, BuildingCheckItemPositionActivity.class);
        }
    }
}
